package com.chenjun.web.controller.oa;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chenjun.common.Constant;
import com.chenjun.common.base.ResultDto;
import com.chenjun.common.bean.Pagination;
import com.chenjun.common.model.DataGridModel;
import com.chenjun.common.model.DataGridSortModel;
import com.chenjun.common.util.DateUtil;
import com.chenjun.core.dao.generator.entity.OaBillAttnDayReport;
import com.chenjun.core.dao.generator.entity.OaBillAttnDetail;
import com.chenjun.core.dao.generator.entity.SysUser;
import com.chenjun.core.service.oa.OaBillAttnDayReportService;
import com.chenjun.core.service.oa.OaBillAttnDetailService;
import com.chenjun.core.service.sys.SysUserService;
import com.ibm.icu.math.BigDecimal;

/**
 * @Description: 排班查询
 * @author dongyibin
 * @date 2017-8-30 上午11:03:07
 */
@Controller
@SuppressWarnings("serial")
@RequestMapping("scheduleSelect")
public class scheduleSelectController {
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private OaBillAttnDayReportService dayReportService;
	@Autowired
	private OaBillAttnDetailService oaBillAttnDetailService;
	
	@RequestMapping("/list.html")
	public String index() {
		return "/attendanceSelect/scheduleSelect";
	}
	
	/**
	 * @Description: 排班查询
	 * @param request
	 * @param param
	 * @return
	 * @author dongyibin
	 * @date 2017-9-18 上午12:07:41
	 */
	@RequestMapping("/search")
	@ResponseBody
	public ResultDto getAttendanceDetail(HttpServletRequest request, DataGridModel param) {
		ResultDto rtDto = new ResultDto();
		String ids= param.getParams().get("userIds").toString();
		param.getParams().put("userIds", Arrays.asList(ids.split(","))); 
		param.getSortBy().add(new DataGridSortModel("clock_time", DataGridSortModel.ASC));
		Pagination data = oaBillAttnDetailService.doPagination(param);
		rtDto.setData(data);
		return rtDto;
	}
	
	private boolean setOaBillAttnDetail(OaBillAttnDetail oaBillAttnDetail){

		// ******
		// user表暂时没有emplCode字段，用id取
		// ******
		String id = oaBillAttnDetail.getEmplCode();
		SysUser user = sysUserService.get(id);
		if(user == null){
			return false;
		}
		
		oaBillAttnDetail.setNo(id);
		oaBillAttnDetail.setEmplId(id);
		oaBillAttnDetail.setEmplCode(null); // 待补充...
		oaBillAttnDetail.setEmplName(user.getRealName());
		oaBillAttnDetail.setManHoursTypeId(null);	// 待补充...
		oaBillAttnDetail.setManHoursTypeName(null); // 待补充...
		oaBillAttnDetail.setAttnTypeId(Constant.ATTENDANCE_STYLE_ARTIFICIAL);
		oaBillAttnDetail.setAttnTypeName("人工补录");
		oaBillAttnDetail.setClockIp("127.0.0.1");
		oaBillAttnDetail.setBillDate(new Date());
		return true;
	}
	
	/**
	 * 
	 * ####确保添加、删除前结算过当日日报，这里判断没有会创建日结数据，不然会与定时产生日结数据冲突，存在两条日结数据
	 * 
	 * @Description: 添加人工补录到考勤明细表，同步更新日结表，月结表
	 * @param request
	 * @param oaBillAttnDetail
	 * @return
	 * @author dongyibin
	 * @date 2017-8-16 下午10:48:55
	 */
	@RequestMapping("/add")
	@ResponseBody
	public ResultDto addAttnDetail(HttpServletRequest request, OaBillAttnDetail oaBillAttnDetail){
		ResultDto rtDto = new ResultDto();
		boolean tag = setOaBillAttnDetail(oaBillAttnDetail);
		if(!tag){
			rtDto.resultError();
			rtDto.setMessage("用户不存在");
			return rtDto;
		}
		// 取此员工的其他考勤数据，计算它的最新工作时数
		// 新的工作时数更新日结表，取旧的工作时数与新的工作时间差值，更新月结表
		
		int rows = oaBillAttnDetailService.create(oaBillAttnDetail);
		if(rows <= 0) {
			rtDto.resultError();
			rtDto.setMessage("编辑系统异常");
			return rtDto;
		}
		// 计算打卡时间，得到正常工作小时
		float newNormalHours = 0f;
		float oldNormalHours = 0f;
		String userid = oaBillAttnDetail.getEmplId();
		Date clockTime = oaBillAttnDetail.getClockTime();
		String billDate = DateUtil.dateToStrShort(clockTime);
		DataGridModel attnDetailParam = new DataGridModel();
		// 取用户billDate这天的考勤数据
		attnDetailParam.getParams().put("emplId", userid);
		attnDetailParam.getParams().put("startClockTime", billDate + " 00:00:00");
		attnDetailParam.getParams().put("endClockTimeTime", billDate + " 23:59:59");
		attnDetailParam.getSortBy().add(new DataGridSortModel("clock_time", DataGridSortModel.ASC));
		newNormalHours = oaBillAttnDetailService.getNormalHours(attnDetailParam);
		
		float differenceHour = 0f;
		// 更新日结表、月结表
		DataGridModel param = new DataGridModel();
		param.getParams().put("emplId", userid);
		// 取billDate这天的日结数据
		param.getParams().put("startTime", billDate);
		param.getParams().put("endTime", billDate);
		List<OaBillAttnDayReport> oaBillAttnDayReports = dayReportService.doListByCondition(param);
		// 有日结数据更新、没有就忽略
		if(!CollectionUtils.isEmpty(oaBillAttnDayReports)){
			OaBillAttnDayReport oaBillAttnDayReport = oaBillAttnDayReports.get(0);
			oldNormalHours = oaBillAttnDayReport.getNormalHours();
			dayReportService.updateNormalHours(oaBillAttnDayReport, newNormalHours);
			// 工作时数增加了newNormalHours - oldNormalHours个小时
			differenceHour = new BigDecimal(newNormalHours).subtract(new BigDecimal(oldNormalHours)).floatValue();
		}else{ // 没有日结数据，创建日结数据
			// 工作时数增加了newNormalHours个小时
			differenceHour = newNormalHours;
			dayReportService.createDayReport(oaBillAttnDetail, newNormalHours);
		}
		// 更新月结表
		// 待补充...	
		return rtDto;
	}
	
	/**
	 * @Description: 删除人工补录记录
	 * @param request
	 * @param id
	 * @return
	 * @author dongyibin
	 * @date 2017-9-14 下午9:59:01
	 */
	@RequestMapping("/delete/{id}")
	@ResponseBody
	public ResultDto deleteAttnDetail(HttpServletRequest request, @PathVariable int id){
		ResultDto rtDto = new ResultDto();
		OaBillAttnDetail oaBillAttnDetail = oaBillAttnDetailService.get(id);
		if(oaBillAttnDetail == null){
			rtDto.resultError();
			rtDto.setMessage("要删除的记录不存在!");
			return rtDto;
		}
		oaBillAttnDetailService.delete(id);
		// 计算打卡时间，得到正常工作小时
		float newNormalHours = 0f;
		float oldNormalHours = 0f;
		String userid = oaBillAttnDetail.getEmplId();
		Date clockTime = oaBillAttnDetail.getClockTime();
		String billDate = DateUtil.dateToStrShort(clockTime);
		DataGridModel attnDetailParam = new DataGridModel();
		// 取用户billDate这天的考勤数据
		attnDetailParam.getParams().put("emplId", userid);
		attnDetailParam.getParams().put("startClockTime", billDate + " 00:00:00");
		attnDetailParam.getParams().put("endClockTimeTime", billDate + " 23:59:59");
		attnDetailParam.getSortBy().add(new DataGridSortModel("clock_time", DataGridSortModel.ASC));
		newNormalHours = oaBillAttnDetailService.getNormalHours(attnDetailParam);
		
		float differenceHour = 0f;
		// 更新日结表、月结表
		DataGridModel param = new DataGridModel();
		param.getParams().put("emplId", userid);
		// 取billDate这天的日结数据
		param.getParams().put("startTime", billDate);
		param.getParams().put("endTime", billDate);
		List<OaBillAttnDayReport> oaBillAttnDayReports = dayReportService.doListByCondition(param);
		// 有日结数据更新、没有就忽略
		if(!CollectionUtils.isEmpty(oaBillAttnDayReports)){
			OaBillAttnDayReport oaBillAttnDayReport = oaBillAttnDayReports.get(0);
			oldNormalHours = oaBillAttnDayReport.getNormalHours();
			dayReportService.updateNormalHours(oaBillAttnDayReport, newNormalHours);
			// 工作时数增加了newNormalHours - oldNormalHours个小时
			differenceHour = new BigDecimal(newNormalHours).subtract(new BigDecimal(oldNormalHours)).floatValue();
		}else{ // 没有日结数据，创建日结数据
			// 工作时数增加了newNormalHours个小时
			differenceHour = newNormalHours;
			dayReportService.createDayReport(oaBillAttnDetail, newNormalHours);
		}
		// 更新月结表
		// 待补充...	
		return rtDto;
	}
	
}
